<?php
namespace App\Services;


use AlibabaCloud\SDK\Docmindapi\V20220711\Docmindapi;
use AlibabaCloud\SDK\Docmindapi\V20220711\Models\GetDocParserResultRequest;
use AlibabaCloud\SDK\Docmindapi\V20220711\Models\QueryDocParserStatusRequest;
use AlibabaCloud\SDK\Docmindapi\V20220711\Models\SubmitDocParserJobRequest;
use AlibabaCloud\SDK\Imm\V20200930\Models\DetectImageTextsRequest;
use AlibabaCloud\SDK\Imm\V20200930\Models\GetTaskRequest;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use AlibabaCloud\SDK\Imm\V20200930\Imm;
use AlibabaCloud\SDK\Imm\V20200930\Models\CreateOfficeConversionTaskRequest;


class AliyunService {
    protected $config;

    public function __construct()
    {
        $this->config = new Config();
        // 访问的域名，支持ipv4和ipv6两种方式，ipv6请使用docmind-api-dualstack.cn-hangzhou.aliyuncs.com
        $this->config->endpoint = "docmind-api.cn-hangzhou.aliyuncs.com";
        // 通过credentials获取配置中的AccessKey ID
        $this->config->accessKeyId = config('services.oss.accessKeyId');
        // 通过credentials获取配置中的AccessKey Secret
        $this->config->accessKeySecret = config('services.oss.accessKeySecret');
        $this->config->type = "access_key";
        $this->config->regionId = config('services.oss.region');

    }

    public function submitDocToAliyun($file,$filename){

        $client = new Docmindapi($this->config);
        $request = new SubmitDocParserJobRequest();

        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout = 10000;

        $request->fileUrl = $file;
        $request->fileName = $filename;

        try {
            $res = $client->submitDocParserJob($request,$runtime);
            return ['statuscode'=>200,'body'=>json_decode(json_encode($res->body),true)];
        } catch (\Exception $e) {
            return $e;
        }
    }

    /**
     * 查询文档分析进度
     * @param $file
     * @param $filename
     * @return array|\Exception
     */
    public function queryDocToAli($doc_id){

        $client = new Docmindapi($this->config);
        $request = new QueryDocParserStatusRequest();
        $request->id = $doc_id;

        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout = 10000;


        try {
            $res = $client->queryDocParserStatus($request,$runtime);
//            if($res->body->data->status == 'success'){
//                $doc_res = $this->getDocResForAli($doc_id);
//
//                return ['statuscode'=>202,'body'=>json_decode(json_encode($doc_res['body']),true)];
//            }
            return ['statuscode'=>200,'body'=>json_decode(json_encode($res->body),true)];
        } catch (\Exception $e) {
            return $e;
        }
    }

    public function getDocResForAli($doc_id,$number)
    {

        $client = new Docmindapi($this->config);
        $request = new GetDocParserResultRequest();
        $request->id = $doc_id;
        $request->layoutStepSize = $number;
        $request->layoutNum = 0;

        $runtime = new RuntimeOptions();
        $runtime->maxIdleConns = 3;
        $runtime->connectTimeout = 10000;
        $runtime->readTimeout = 10000;


        try {
            $res = $client->getDocParserResult($request, $runtime);
            return ['statuscode'=>200,'body'=>json_decode(json_encode($res->body),true)];
        } catch (\Exception $e) {
            return $e;
        }
    }

    /**
     * 提交文档进行图片转换
     */
    public function createOfficeConversionTask($page,$file,$file_name){
        $this->config->endpoint = "imm.cn-hangzhou.aliyuncs.com";
        $client = new Imm($this->config);
        //组装数据
        $pages = $page.'-'.$page;
        $bucket = env('ALI_OSS_BUCKET_NAME');
        $oss_url = "oss://".$bucket.'/'.$file;
        $new_file_name = explode('.',$file_name);
        $target_url = "oss://".$bucket."/".$new_file_name[0].".jpg";
        //正式请求
        $createOfficeConversionTaskRequest = new CreateOfficeConversionTaskRequest();
        $createOfficeConversionTaskRequest->projectName = 'PDF_S';
        $createOfficeConversionTaskRequest->targetType = 'jpg';
        $createOfficeConversionTaskRequest->pages = $pages;
        $createOfficeConversionTaskRequest->sourceURI = $oss_url;
        $createOfficeConversionTaskRequest->targetURI = $target_url;
        $createOfficeConversionTaskRequest->targetURIPrefix = $target_url;
        $runtime = new RuntimeOptions([]);

            // 复制代码运行请自行打印 API 的返回值
        $result = $client->createOfficeConversionTaskWithOptions($createOfficeConversionTaskRequest, $runtime);
        return ['statuscode'=>200,'body'=>json_decode(json_encode($result->body),true)];

    }

    /**
     * 获取任务结果信息
     * @param task_id 任务ID
     */
    public function getTask($task_id){
        $this->config->endpoint = "imm.cn-hangzhou.aliyuncs.com";
        $client = new Imm($this->config);
        $getTaskRequest = new GetTaskRequest();
        $getTaskRequest->projectName = 'PDF_S';
        $getTaskRequest->taskType = 'OfficeConversion';
        $getTaskRequest->taskId = $task_id;
        $runtime = new RuntimeOptions([]);

        // 复制代码运行请自行打印 API 的返回值
        $result = $client->getTaskWithOptions($getTaskRequest, $runtime);
        return ['statuscode'=>200,'body'=>json_decode(json_encode($result->body),true)];

    }

    public function DetectImageText($file){
        $this->config->endpoint = "imm.cn-hangzhou.aliyuncs.com";
        $client = new Imm($this->config);
        $bucket = env('ALI_OSS_BUCKET_NAME');
        $url = "oss://".$bucket."/".$file;
        $detectImageTextsRequest = new DetectImageTextsRequest();
        $detectImageTextsRequest->projectName = 'PDF_S';
        $detectImageTextsRequest->sourceURI = $url;
        $runtime = new RuntimeOptions([]);

        // 复制代码运行请自行打印 API 的返回值
        $result= $client->detectImageTextsWithOptions($detectImageTextsRequest, $runtime);
        return ['statuscode'=>200,'body'=>json_decode(json_encode($result->body),true)];

    }
}
